package com.ruoyi.btlfbreed.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.btlfbreed.domain.MyTbChickenHouses;
import com.ruoyi.btlfbreed.domain.TbChickenDisinfectionRecords;
import com.ruoyi.btlfbreed.domain.TbEquipment;
import com.ruoyi.btlfbreed.mapper.TbEquipmentMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.btlfbreed.mapper.TbChickenHousesMapper;
import com.ruoyi.btlfbreed.domain.TbChickenHouses;
import com.ruoyi.btlfbreed.service.ITbChickenHousesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * chickenHousesService业务层处理
 * 
 * @author codeman
 * @date 2024-05-03
 */
@Service
public class TbChickenHousesServiceImpl implements ITbChickenHousesService 
{
    @Autowired
    private TbChickenHousesMapper tbChickenHousesMapper;
    @Autowired
    private TbEquipmentMapper tbEquipmentMapper;

    /**
     * 查询chickenHouses
     * 
     * @param houseId chickenHouses主键
     * @return chickenHouses
     */
    @Override
    public TbChickenHouses selectTbChickenHousesByHouseId(Long houseId)
    {
        return tbChickenHousesMapper.selectTbChickenHousesByHouseId(houseId);
    }

    /**
     * 查询chickenHouses列表
     * 
     * @param tbChickenHouses chickenHouses
     * @return chickenHouses
     */
    @Override
    public List<TbChickenHouses> selectTbChickenHousesList(TbChickenHouses tbChickenHouses)
    {
        return tbChickenHousesMapper.selectTbChickenHousesList(tbChickenHouses);
    }

    /**
     * 新增chickenHouses
     * 
     * @param tbChickenHouses chickenHouses
     * @return 结果
     */
    @Override
    public int insertTbChickenHouses(TbChickenHouses tbChickenHouses)
    {
        tbChickenHouses.setCreateTime(DateUtils.getNowDate());
        int rows = tbChickenHousesMapper.insertTbChickenHouses(tbChickenHouses);
        insertTbChickenDisinfectionRecords(tbChickenHouses);
        return rows;
    }

    /**
     * 修改chickenHouses
     * 
     * @param tbChickenHouses chickenHouses
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbChickenHouses(TbChickenHouses tbChickenHouses)
    {
        tbChickenHouses.setUpdateTime(DateUtils.getNowDate());
        if (!tbChickenHouses.getStatus().equals("1"))
        {
            TbEquipment temp = new TbEquipment();
            temp.setHouseId(tbChickenHouses.getHouseId());
            List<TbEquipment> tbEquipments = tbEquipmentMapper.selectTbEquipmentList(temp);
            for (TbEquipment item :
                tbEquipments)
            {
                item.setStatus(tbChickenHouses.getStatus());
                tbEquipmentMapper.updateTbEquipment(item);
            }
        }
        tbChickenHousesMapper.deleteTbChickenDisinfectionRecordsByChickenHouseId(tbChickenHouses.getHouseId());
        insertTbChickenDisinfectionRecords(tbChickenHouses);
        return tbChickenHousesMapper.updateTbChickenHouses(tbChickenHouses);
    }

    /**
     * 批量删除chickenHouses
     * 
     * @param houseIds 需要删除的chickenHouses主键
     * @return 结果
     */
    @Override
    public int deleteTbChickenHousesByHouseIds(Long[] houseIds)
    {
        tbChickenHousesMapper.deleteTbChickenDisinfectionRecordsByChickenHouseIds(houseIds);

        return tbChickenHousesMapper.deleteTbChickenHousesByHouseIds(houseIds);
    }

    /**
     * 删除chickenHouses信息
     * 
     * @param houseId chickenHouses主键
     * @return 结果
     */
    @Override
    public int deleteTbChickenHousesByHouseId(Long houseId)
    {
        tbChickenHousesMapper.deleteTbChickenDisinfectionRecordsByChickenHouseId(houseId);
        return tbChickenHousesMapper.deleteTbChickenHousesByHouseId(houseId);
    }
    /**
     * 新增消毒记录信息
     *
     * @param tbChickenHouses chickenHouses对象
     */
    public void insertTbChickenDisinfectionRecords(TbChickenHouses tbChickenHouses)
    {
        List<TbChickenDisinfectionRecords> tbChickenDisinfectionRecordsList = tbChickenHouses.getTbChickenDisinfectionRecordsList();
        Long houseId = tbChickenHouses.getHouseId();
        if (StringUtils.isNotNull(tbChickenDisinfectionRecordsList))
        {
            List<TbChickenDisinfectionRecords> list = new ArrayList<TbChickenDisinfectionRecords>();
            for (TbChickenDisinfectionRecords tbChickenDisinfectionRecords : tbChickenDisinfectionRecordsList)
            {
                tbChickenDisinfectionRecords.setChickenHouseId(String.valueOf(houseId));
                list.add(tbChickenDisinfectionRecords);
            }
            if (list.size() > 0)
            {
                tbChickenHousesMapper.batchTbChickenDisinfectionRecords(list);
            }
        }
    }
}
